package com.anhry.app.safety.manage.system.struts.action;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.io.XMLWriter;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.system.bean.TbSysFun;
import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.struts.base.SystemBaseAction;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.Param;
import com.anhry.app.safety.util.Util;
import com.anhry.app.safety.util.bean.Permission;


/**
 * @date:2012-5-16
 * @remark:
 * @author:dongbin
 */

@Controller
@Scope("prototype")
public class MenuAction extends SystemBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private List funList;
	private String funId;
	private TbSysFun fun;
	private String pid;
	private String funName;
	private String funParentId;
	private String funParentName;
	private String funUrl;
	private String funSort;
	private String funTarget;
	private String funRemark;
	private String funType;//菜单访问权限0：超级管理员1：其他管理员
	private String funCode;//菜单编码
	private String funTag;
	private String funState;//是否有子菜单0：无1：有
	
	@Permission(module = "menu", permissionVaule = 1)
	public String list(){
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			this.setFunList(this.systemService.getFunByUser(user));
			return "menu_list";
		} catch (Exception e) {
			Loggers.error("manager:MenuAction:list()方法出错："+e);
			e.printStackTrace();
		}
		return ERROR;
	}
	
	/*
	 * 加载菜单
	 */
	@Permission(module = "menu", permissionVaule = 0)
	public String load(){
		try {
			//如果是修改
			if(this.funId != null && !"".equals(this.funId)){
				TbSysFun fun = (TbSysFun)this.utilService.getObjectById(TbSysFun.class, Long.parseLong(this.funId));
				this.setFun(fun);
			}
			//如果是添加子项
			if(this.pid != null && !"".equals(this.pid)){
				TbSysFun fun = (TbSysFun)this.utilService.getObjectById(TbSysFun.class, Long.parseLong(this.pid));
				TbSysFun fun1 = new TbSysFun();
				fun1.setFunParentId(fun.getId());	//设置父菜单ID
				fun1.setFunParentName(fun.getFunName());	//设置父菜单名称
				this.setFun(fun1);
			}
		} catch (Exception e) {
			Loggers.error("manager:MenuAction:load()方法出错："+e);
			e.printStackTrace();
		}
		return "menu_edit";
	}
	
	/*
	 * 删除菜单
	 */
	@Permission(module = "menu", permissionVaule = 3)
	public String delete(){
		try {
			if(this.funId != null && !"".equals(this.funId)){
				StringBuffer sb = new StringBuffer();	//拼接修改的hql语句,in(1,2,3)
				sb.append(this.funId+",");
				List<TbSysFun> childList = this.systemService.getChildFun(fun);	//获得此菜单下的所有子菜单
				if(childList != null && childList.size() > 0){
					for(TbSysFun childFun : childList){
						sb.append(childFun.getId());
						sb.append(",");
					}
				}
				sb.setLength(sb.length() - 1);	//去,号
				if(sb != null && sb.length() > 0){
					List<Param> paramList = new ArrayList<Param>();
					Param p1 = new Param("id","in",sb);
					paramList.add(p1);
					Map<String, String> setMap = new HashMap<String, String>();
					setMap.put("isDele", "1");
					setMap.put("funRemark", "菜单删除");
					this.utilService.updateAll("TbSysFun", setMap, paramList);
					this.setTip("删除成功!");
				}
				this.createFunXml();
			}
		} catch (Exception e) {
			Loggers.error("manager:MenuAction:delete()方法出错："+e);
			e.printStackTrace();
		}
		return this.list();
	}
	
	/**
	 * 添加或修改菜单
	 * @return
	 */
	public String save(){
		TbSysFun fun = null;
		try {
			if(this.getFunId() != null && !"".equals(this.getFunId())){	//修改
				fun = (TbSysFun)this.utilService.getObjectById(TbSysFun.class, Long.parseLong(this.getFunId()));
				if (fun != null) {
					if(this.getFunParentId() != null && !"".equals(this.getFunParentId())){
						TbSysFun parent = (TbSysFun)this.utilService.getObjectById(TbSysFun.class, Long.parseLong(this.getFunParentId()));
						if(parent != null){
							fun.setFunParentId(Long.parseLong(this.getFunParentId()));
							fun.setFunParentName(parent.getFunName());
						}else{
							this.setTip("父节点不存在，保存失败！");
							return this.list();
						}
					}
					fun.setFunUrl(this.funUrl);
					fun.setFunCode(this.funCode);
					fun.setFunState(this.funState);
					fun.setFunType(this.funType);
					if(this.getFunSort() == null || "".equals(this.getFunSort())){
						fun.setFunSort(Long.parseLong("0"));
					}else{
						fun.setFunSort(Long.parseLong(this.getFunSort()));
					}
					if(this.getFunTarget() == null || "".equals(this.getFunTarget())){
						fun.setFunTarget("rightFrame");
					}else{
						fun.setFunTarget(this.getFunTarget());
					}
					fun.setFunName(this.getFunName());
					fun.setFunRemark("更新菜单");
					fun.setIsDele("0");
			        this.utilService.update(fun);
			        this.setTip("修改菜单成功！");
			        this.createFunXml();
				}
			}else{	//添加
				fun = new TbSysFun();
				if(this.getFunParentId() != null && !"".equals(this.getFunParentId())){
					TbSysFun parent = (TbSysFun)this.utilService.getObjectById(TbSysFun.class, Long.parseLong(this.getFunParentId()));
					if(parent != null){
						fun.setFunParentId(Long.parseLong(this.getFunParentId()));
						fun.setFunParentName(parent.getFunName());
					}else{
						this.setTip("父节点不存在，保存失败！");
						return this.list();
					}
				}
				if(this.getFunSort() == null || "".equals(this.getFunSort())){
					fun.setFunSort(Long.parseLong("0"));
				}else{
					fun.setFunSort(Long.parseLong(this.getFunSort()));
				}
				if(this.getFunTarget() == null || "".equals(this.getFunTarget())){
					fun.setFunTarget("rightFrame");
				}else{
					fun.setFunTarget(this.getFunTarget());
				}
				fun.setFunUrl(this.funUrl);
				fun.setFunCode(this.funCode);
				fun.setFunState(this.funState);
				fun.setFunType(this.funType);
				fun.setFunName(this.funName);
				fun.setFunRemark("添加菜单");
				fun.setIsDele("0");
		        this.utilService.save(fun);
		        this.setTip("添加菜单成功！");
		        this.createFunXml();
			}	
			return this.list();
		} catch (Exception e) {
			Loggers.error("manager:MenuAction:save()方法出错："+e);
			e.printStackTrace();
		}				
		return ERROR;
	}
	
	/**
	 * 生成检查项xml文件
	 *
	 */
	public void createFunXml(){
		try {
			XMLWriter writer = new XMLWriter(new FileOutputStream(this.contextPvd.getAppRealPath("")+"/dtree/fun_tree.xml"));
			writer.write(this.systemService.funTreeInit(null,null)); 
			writer.close(); 
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("XzzfConfigAction 中 createChitem()方法："+e.getMessage());
		}
	}
	
	public TbSysFun getFun() {
		return fun;
	}

	public void setFun(TbSysFun fun) {
		this.fun = fun;
	}

	public String getFunId() {
		return funId;
	}

	public void setFunId(String funId) {
		this.funId = funId;
	}

	public List getFunList() {
		return funList;
	}

	public void setFunList(List funList) {
		this.funList = funList;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getFunCode() {
		return funCode;
	}

	public void setFunCode(String funCode) {
		this.funCode = funCode;
	}

	public String getFunName() {
		return funName;
	}

	public void setFunName(String funName) {
		this.funName = funName;
	}

	public String getFunParentId() {
		return funParentId;
	}

	public void setFunParentId(String funParentId) {
		this.funParentId = funParentId;
	}

	public String getFunParentName() {
		return funParentName;
	}

	public void setFunParentName(String funParentName) {
		this.funParentName = funParentName;
	}

	public String getFunRemark() {
		return funRemark;
	}

	public void setFunRemark(String funRemark) {
		this.funRemark = funRemark;
	}

	public String getFunSort() {
		return funSort;
	}

	public void setFunSort(String funSort) {
		this.funSort = funSort;
	}

	public String getFunState() {
		return funState;
	}

	public void setFunState(String funState) {
		this.funState = funState;
	}

	public String getFunTag() {
		return funTag;
	}

	public void setFunTag(String funTag) {
		this.funTag = funTag;
	}

	public String getFunTarget() {
		return funTarget;
	}

	public void setFunTarget(String funTarget) {
		this.funTarget = funTarget;
	}

	public String getFunType() {
		return funType;
	}

	public void setFunType(String funType) {
		this.funType = funType;
	}

	public String getFunUrl() {
		return funUrl;
	}

	public void setFunUrl(String funUrl) {
		this.funUrl = funUrl;
	}

}
